package puyu.manage.service.file.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import puyu.manage.service.file.dao.SysTFileUploadMapper;
import puyu.manage.service.file.entity.SysTFileCenter;
import puyu.manage.service.file.dao.SysTFileCenterMapper;
import org.springframework.stereotype.Service;
import puyu.manage.service.file.entity.SysTFileUpload;
import puyu.manage.service.file.entity.vo.FileUploadVo;
import puyu.manage.service.minio.MinioUtil;
import puyu.manage.service.report.dao.ReportDao;
import puyu.manage.service.sample.dao.SampleDao;
import puyu.manage.service.utils.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 文件中间表(SysTFileCenter)表服务实现类
 *
 * @author makejava
 * @since 2024-06-07 15:12:41
 */
@Service
public class SysTFileCenterServiceImpl extends ServiceImpl<SysTFileCenterMapper, SysTFileCenter> implements SysTFileCenterService {

    @Resource
    private SysTFileCenterMapper sysTFileCenterMapper;

    @Resource
    private SysTFileCenterService sysTFileCenterService;

    @Resource
    private SysTFileUploadMapper sysTFileUploadMapper;

    @Resource
    private SysTFileUploadService sysTFileUploadService;

    @Resource
    private MinioUtil minioUtil;

    @Resource
    private SampleDao sampleDao;

    @Resource
    private ReportDao reportDao;

    @Override
    public void saveByFile(FileUploadVo vo) {

        List<SysTFileCenter> centers = new ArrayList<>();

        //业务ID
        String id = vo.getId();

        //文件ID
        List<String> fileIds = vo.getFileIds();

        if (ObjectUtil.isNotEmpty(fileIds)){

            for (String fileId : fileIds) {

                SysTFileCenter center = new SysTFileCenter();

                //文件ID
                center.setFileId(fileId);

                //业务ID
                center.setServiceId(id);

                centers.add(center);
            }

            this.saveBatch(centers);
        }


    }

    /**
     * 通过业务ID查询文件
     * id 业务ID
     **/
    @Override
    public List<SysTFileUpload> getByFile(String id) {

        List<SysTFileUpload> sysTFileUploads = new ArrayList<>();

        List<SysTFileCenter> sysTFileCenters = sysTFileCenterMapper.selectList(new LambdaQueryWrapper<SysTFileCenter>()
                .eq(SysTFileCenter::getServiceId, id));

        if (ObjectUtil.isNotEmpty(sysTFileCenters)){

            List<String> files = sysTFileCenters.stream().map(v -> v.getFileId()).collect(Collectors.toList());

            sysTFileUploads = sysTFileUploadMapper.selectList(new LambdaQueryWrapper<SysTFileUpload>()
                    .in(SysTFileUpload::getFileId,files));

        }

        return sysTFileUploads;
    }

    /**
     * 根据业务ID删除文件数据
     **/
    @Override
    public void deleteByFiles(String id) {

        //通过业务ID查询文件书
        List<SysTFileCenter> sysTFileCenters = sysTFileCenterMapper.selectList(new LambdaQueryWrapper<SysTFileCenter>()
                .eq(SysTFileCenter::getServiceId, id));

        if (ObjectUtil.isNotEmpty(sysTFileCenters)){
            //关联表数据
            List<String> centerId = sysTFileCenters.stream().map(v -> v.getId()).collect(Collectors.toList());
            sysTFileCenterService.removeByIds(centerId);

            //文件ID
            List<String> fileIds = sysTFileCenters.stream().map(v -> v.getFileId()).collect(Collectors.toList());
            //文件服务器
            List<SysTFileUpload> sysTFileUploads = sysTFileUploadMapper.selectList(new LambdaQueryWrapper<SysTFileUpload>()
                    .in(SysTFileUpload::getFileId, fileIds));
            sysTFileUploadService.removeByIds(fileIds);


            for (SysTFileUpload file : sysTFileUploads) {
                String name = file.getFileName();
                minioUtil.removeObject(name);
            }



        }

    }


    /**
     * 根据业务数据删除
     **/
    @Override
    public void deleteByFileName(SysTFileUpload sysTFileUpload) {

        if (ObjectUtil.isNotEmpty(sysTFileUpload)){

            //根据文件ID删除
            List<SysTFileCenter> sysTFileCenters = sysTFileCenterMapper.selectList(new LambdaQueryWrapper<SysTFileCenter>()
                    .eq(SysTFileCenter::getFileId, sysTFileUpload.getFileId()));

            sysTFileCenterService.removeByIds(sysTFileCenters.stream().map(SysTFileCenter::getId).collect(Collectors.toList()));

            //删除文件
            sysTFileUploadService.removeById(sysTFileUpload.getFileId());

            //根据minio名称删除数据
            String fileName = sysTFileUpload.getFileName();
            minioUtil.removeObject(fileName);


        }


    }

    /**
     * 样品根据url删除文件数据
     * videoType = 1删除视频文件  2二维码图片
     * serverType = sample查询样品url serverType = report查询报告二维码url
     **/
    @Override
    public void deleteByFile(String id,String videoType,String serverType) {

        //通过业务ID查询URL
        Map<String, Object> map = sampleDao.queryById(id);

        String path = "";
        if("1".equals(videoType) && "sample".equals(serverType)){//删除视频
            path = (String) map.get("videoPath");
        }

        if ("2".equals(videoType) && "sample".equals(serverType)){//删除二维码
            path = (String) map.get("sampleCodePath");
        }

        if ("2".equals(videoType) && "report".equals(serverType)){//删除报告二维码
            Map<String, Object> report = reportDao.queryById(id);
            path = (String) report.get("reportCodePath");
        }
        this.deleteByUrl(path);


        }





    //根据URL删除minio数据和文件库数据
    public void deleteByUrl(String path){
        if (StringUtils.isNotEmpty(path)){
            //通过URL查询文件
            SysTFileUpload fileUpload = sysTFileUploadMapper.selectOne(new LambdaQueryWrapper<SysTFileUpload>()
                    .eq(SysTFileUpload::getFileUrl, path));

            if (ObjectUtil.isNotEmpty(fileUpload)){

                String fileId = fileUpload.getFileId();

                String name = fileUpload.getFileName();
                minioUtil.removeObject(name);

                List<SysTFileCenter> sysTFileCenters = sysTFileCenterMapper.selectList(new LambdaQueryWrapper<SysTFileCenter>()
                        .eq(SysTFileCenter::getFileId, fileId));

                //删除关联表
                if (ObjectUtil.isNotEmpty(sysTFileCenters)){
                    sysTFileCenterService.removeByIds(sysTFileCenters.stream().map(SysTFileCenter::getId).collect(Collectors.toList()));
                }

                //删除文件
                sysTFileUploadService.removeById(fileId);

            }


        }
    }

}

